package com.xdtech.parking.service.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xdtech.common.utils.DateUtil;
import com.xdtech.common.utils.DateUtil.DateStyle;
import com.xdtech.core.dao.BaseDao;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.parking.dao.ParkOrderInfoDao;
import com.xdtech.parking.model.ParkOrderInfo;
import com.xdtech.parking.service.ParkOrderInfoService;
import com.xdtech.parking.service.ParkingDeviceService;
import com.xdtech.parking.util.ParkingContants;
import com.xdtech.parking.vo.BillDetail;
import com.xdtech.parking.vo.ParkOrderInfoItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-06-28 19:18:42
 * @since 1.0
 * @see
 */
@Service
public class ParkOrderInfoServiceImpl implements ParkOrderInfoService {
	private Log log = LogFactory.getLog(ParkOrderInfoServiceImpl.class);
	@Autowired
	private ParkOrderInfoDao parkOrderInfoDao;
	@Autowired
	private BaseDao baseDao;
	@Autowired
	private ParkingDeviceService parkingDeviceService;
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-28 19:18:42
	 * @modified by
	 * @param entity
	 */
	public void save(ParkOrderInfo entity) {
		parkOrderInfoDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-28 19:18:42
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<ParkOrderInfo> entities) {
		parkOrderInfoDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-28 19:18:42
	 * @modified by
	 * @param entity
	 */
	public void delete(ParkOrderInfo entity) {
		parkOrderInfoDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-28 19:18:42
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		parkOrderInfoDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-28 19:18:42
	 * @modified by
	 * @param id
	 * @return
	 */
	public ParkOrderInfo get(Long id) {
		return parkOrderInfoDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-28 19:18:42
	 * @modified by
	 * @return
	 */
	public List<ParkOrderInfo> getAll() {
		return parkOrderInfoDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-28 19:18:42
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> parkOrderInfos = null;
		long rows = 0;
		if (pg!=null) {
			Page<ParkOrderInfo> page = parkOrderInfoDao.findPage(pg,"from ParkOrderInfo order by createTime desc", values);
			parkOrderInfos = BeanUtils.copyListProperties(
					ParkOrderInfoItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<ParkOrderInfo> parkOrderInfoList = parkOrderInfoDao.find("from ParkOrderInfo order by id desc", values);
			parkOrderInfos = BeanUtils.copyListProperties(
					ParkOrderInfoItem.class, parkOrderInfoList);
			rows = parkOrderInfos.size();
		}
		results.put("total", rows);
		results.put("rows", parkOrderInfos);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-28 19:18:42
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateParkOrderInfo(ParkOrderInfoItem item) {
		ParkOrderInfo parkOrderInfo = null;
		if (item.getId()==null) {
			parkOrderInfo = new ParkOrderInfo();
		}else {
			parkOrderInfo = parkOrderInfoDao.get(item.getId());
		}
		//复制前台修改的属性
		BeanUtils.copyProperties(parkOrderInfo, item);
		parkOrderInfoDao.save(parkOrderInfo);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-28 19:18:42
	 * @modified by
	 * @param parkOrderInfoId
	 * @return
	 */
	public ParkOrderInfoItem loadParkOrderInfoItem(Long parkOrderInfoId) {
		ParkOrderInfo parkOrderInfo = parkOrderInfoDao.get(parkOrderInfoId);
		return ParkOrderInfoItem.createItem(parkOrderInfo);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-28 19:18:42
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteParkOrderInfoInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-28 19:18:42
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteParkOrderInfoInfo(List<Long> parkOrderInfoIds) {
		for (Long id : parkOrderInfoIds) {
			delete(id);
		}
		return true;
	}
	
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		Map<String, Object> results = new HashMap<String, Object>();
		Page page = baseDao.findPageByNamedQuery(pg, queryName,baseItem);
		results.put("total", page.getTotalItems());
		results.put("rows", page.getResult());
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-28 19:18:42
	 * @modified by
	 * @return
	 */
	@Override
	public List<ParkOrderInfoItem> loadItems() {
		List<ParkOrderInfo> parkOrderInfos = getAll();
		return ParkOrderInfoItem.createItems(parkOrderInfos);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-6-28下午9:25:54
	 * @modified by
	 * @param billDetail
	 */
	@Override
	public void savePreOrderInfo(BillDetail billDetail,String telephone) {
		ParkOrderInfo parkOrderInfo = new ParkOrderInfo();
		parkOrderInfo.setAreaCode(billDetail.getAid());
		parkOrderInfo.setDeviceCode(billDetail.getParkNo());
		parkOrderInfo.setOrderNo(billDetail.getSysOrderNo());
		parkOrderInfo.setPayAmount(Double.valueOf(billDetail.getTotal()));
		parkOrderInfo.setPayType(ParkingContants.PARK_WEIXIN_TYPE);
		parkOrderInfo.setWxName("wxName");
		//0待支付 1支付成功 2支付失败  
		parkOrderInfo.setPayResult("0");
		parkOrderInfo.setPayOrderNo(billDetail.getPrepayId());
		parkOrderInfo.setPaySign(billDetail.getSign());
		//设置付款操作电话
		parkOrderInfo.setTelephone(telephone);
		save(parkOrderInfo);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-7-1下午11:49:55
	 * @modified by
	 * @param payPreId
	 * @param telephone
	 */
	@Override
	public void update(String payPreId, String telephone) {
		ParkOrderInfo parkOrderInfo = parkOrderInfoDao.findUnique("from ParkOrderInfo where payOrderNo=?", payPreId);
		//后续这些不是由我们判断，有第三方支付中心接口调用判断
		parkOrderInfo.setPayResult("1");
//		parkOrderInfo.setTelephone(telephone);
		parkOrderInfo.setPayTime(DateUtil.getDate(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
		
		String deviceUnique = parkOrderInfo.getAreaCode()+"_"+parkOrderInfo.getDeviceCode();
		log.info("操作设备："+deviceUnique);
		//支付成功后，锁降下来
		parkingDeviceService.turnOff(parkOrderInfo.getAreaCode(),parkOrderInfo.getDeviceCode());
		
		save(parkOrderInfo);
	}

}
